package com.meixiang.adapter.moments;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.moments.MomentsRecommendList;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.flowlayout.CustomFlowAdapter;
import com.meixiang.view.flowlayout.CustomFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 推荐
 * 作者：黑哥 on 2016/5/7 14:34
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> {

    private Context context;
    private Activity mActivity;
    private List<MomentsRecommendList> mList = new ArrayList<>();

    public List<MomentsRecommendList> getData(){
        return mList;
    }

    public RecommendAdapter(Context context, Activity mActivity) {
        this.context = context;
        this.mActivity = mActivity;
    }

    public void addAll(List<MomentsRecommendList> orderDetails) {
        this.mList.addAll(orderDetails);
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.mList.clear();
        this.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<MomentsRecommendList> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.attention_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
            GlideHelper.showCircleImage(mActivity, mList.get(position).getMemberAvatar(), R.mipmap.default_head_portrait, ((ViewHolder) holder).mIvUserPortrait);
            holder.mTvUserName.setText(mList.get(position).getMemberName());
//            holder.mTvContent.setText(mList.get(position).getComment());
            if(mList.get(position).getComment().equals("")||mList.get(position).getComment().length()==0){
                holder.mTvContent.setVisibility(View.GONE);
            }else{
                holder.mTvContent.setText(mList.get(position).getComment());
            }
            holder.mTvTime.setText(mList.get(position).getCreateTime());
            holder.mTvLike.setText(mList.get(position).getLaudNumber());
            holder.mTvMessage.setText(mList.get(position).getCommentNumber());
            holder.mCustomFlowLayout.setMargin(0, 5, 5, 0);
            final String[] urls = mList.get(position).getCommentImages();
            if (urls == null || urls.length == 0) {
                holder.mCustomFlowLayout.setVisibility(View.GONE);
            } else {
                holder.mCustomFlowLayout.setVisibility(View.VISIBLE);
                holder.mCustomFlowLayout.setAdapter(new CustomFlowAdapter(urls) {
                    @Override
                    public View getView(CustomFlowLayout parent, int position, Object o) {
                        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_attention_image, parent, false);
                        ImageView imageView = (ImageView) layout.findViewById(R.id.iv_image);
                        GlideHelper.showImage(context, (String) o, R.drawable.image_default_gray_bg, imageView);
                        return layout;
                    }
                });

            }

            if (mList.get(position).getMyLaud().equals("1")) {
                holder.mIvLike.setImageResource(R.mipmap.praise_up);
                holder.mTvLike.setTextColor(ContextCompat.getColor(context, R.color.magenta));
            } else {
                holder.mIvLike.setImageResource(R.mipmap.praise);
                holder.mTvLike.setTextColor(ContextCompat.getColor(context, R.color.black_9));
            }

        if(MXApplication.mApp.getUser()!=null&&!AbStrUtil.isEmpty(MXApplication.mApp.getUser().getMemberId())){
            if (mList.get(position).getMemberId().equals(MXApplication.mApp.getUser().getMemberId())){
                holder.mTvAttention.setVisibility(View.GONE);
            }else{
                holder.mTvAttention.setVisibility(View.VISIBLE);
                if (mList.get(position).getMyConcern().equals("1")) {
                    holder.mTvAttention.setText(R.string.moments_has_attention);
                } else {
                    holder.mTvAttention.setText(R.string.moments_attention);
                }
            }
        }

        //点赞点击事件


        holder.mIvLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onLikeItemClick(v, position, holder.mTvLike);
                }
            }
        });

        holder.mTvAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onAttentionItemClick(v, position);
                }
            }
        });
        //评论点击事件


        holder.mIvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onMessageItemClick(v, position);
                }
            }
        });

        holder.mLlCheckDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCheckoutPositionItemClick(v, position);
                }
            }
        });


    }

    /**
     * 单击事件监听器
     */
    private onItemClickListener mListener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        mListener = listener;
    }

    public interface onItemClickListener {

        /**
         * 点赞点击事件
         *
         * @param v
         * @param position
         */
        void onLikeItemClick(View v, int position,TextView mTvLike);

        /**
         * 关注点击事件
         *
         * @param v
         * @param position
         */
        void onAttentionItemClick(View v, int position);

        /**
         * 查看详情点击事件
         *
         * @param v
         * @param position
         */
        void onCheckoutPositionItemClick(View v, int position);

        /**
         * 消息点击事件
         *
         * @param v
         * @param position
         */
        void onMessageItemClick(View v, int position);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_user_portrait)
        ImageView mIvUserPortrait;
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        @Bind(R.id.tv_content)
        TextView mTvContent;
        @Bind(R.id.ll_check_details)
        LinearLayout mLlCheckDetails;
        @Bind(R.id.tv_time)
        TextView mTvTime;
        @Bind(R.id.iv_like)
        ImageView mIvLike;
        @Bind(R.id.tv_like)
        TextView mTvLike;
        @Bind(R.id.iv_message)
        ImageView mIvMessage;
        @Bind(R.id.tv_message)
        TextView mTvMessage;
        @Bind(R.id.tv_attention)
        TextView mTvAttention;
        @Bind(R.id.flow_image)
        CustomFlowLayout mCustomFlowLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
